var express = require('express')
var User = require('./models/user')
var md5 = require('blueimp-md5')
const user = require('./models/user')
var Topic = require('./models/topic')

var router =  express.Router()

router.get('/', function (req, res,next) {
    var topic;
    var p = new Promise(function (resolve, reject) {
        Topic.find(function (err, data) {
            if (err) {
                reject(err)
            } else {
                resolve(data)
            }
        })
    })

    p.then(function(data){
        topic = data
        res.render('index.html', {
            user: req.session.user,
            topic: topic
        })
    },function(error){
        return next(error)
    })
    
    
})

router.get('/login', function (req, res) {
    res.render('login.html')
})

router.post('/login', function (req, res, next) {
    var body = req.body
    User.findOne({
        email: body.email,
        password: md5(md5(body.password))
    },function(err,user){
        if(err){
            // return res.status(500).json({
            //     err_code: 500,
            //     message: err.message
            // })
            return next(err)
        }

        if(!user){
            return res.status(200).json({
                err_code: 1,
                message: 'Email or password is invalid'
            })
            return next(err)
        }

        req.session.user = user

        res.status(200).json({
            err_code: 0,
            message: 'OK'
        })

    })
})

router.get('/register', function (req, res, next) {
    res.render('register.html')
})

router.post('/register', function (req, res, next) {
    var user = req.body
    User.findOne({
        $or:[
            {
                email: user.email
            },
            {
                nickname: user.nickname
            }
        ]
    },function(err,data){
        if(err){
            return next(err)
        }
        if(data){
            return res.status(200).json({
                err_code: 1,
                message: '邮箱或昵称已存在！'
            })
        }

        user.password = md5(md5(user.password))
        new User(user).save(function(err,data){
            if (err) {
                // return res.status(500).json({
                //     err_code: 500,
                //     message: '服务器错误！'
                // })
                return next(err)
            } 
        })

        //注册成功，使用session记录用户登录状态
        req.session.user = user

        res.status(200).json({
            err_code: 0,
            message: 'ok'
        })
        

    })
})

router.get('/logout',function(req,res){
    req.session.user = null
    res.redirect('/login')
})

router.get('/topics/new',function(req,res){
    res.render('topic/new.html', {
        user:req.session.user
    })
})

router.post('/topics/new',function(req,res,next){
    var topic = req.body
    new Topic(topic).save(function(err,data){
        if(err){
            return next(err)
        }
        res.status(200).json({
            err_code: 200,
            message: 'OK'
        })
    })
})

module.exports = router